package com.bw.user.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.IntentAction;
import com.bw.shopping.mvvm.view.PayActivity;
import com.bw.user.R;
import com.bw.user.adapter.OrderListAdapter;
import com.bw.user.entity.CancelEntity;
import com.bw.user.entity.ConfirmEntity;
import com.bw.user.entity.OrderListEntity;
import com.bw.user.entity.PaysinEntity;
import com.bw.user.mvvm.model.OrderListModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/11/2 14:20
 */
public class OrderListViewModel extends BaseViewModel<OrderListModel> {

    public OrderListAdapter orderListAdapter = new OrderListAdapter();
    List<UserEntity> list;
    @Override
    public void oncreate() {
        super.oncreate();
        EventBus.getDefault().register(this);

        list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderStatus",0);
        hashMap.put("userId",list.get(0).getUser_id());
        m.requestObserList(hashMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof OrderListEntity){
            OrderListEntity orderListEntity = (OrderListEntity) baseEntity;
            orderListAdapter.setNewInstance(orderListEntity.getData());
            orderListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                    if(R.id.item_allorder_cancle == view.getId()){
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("orderId",orderListAdapter.getItem(position).getId());
                        m.requestCannel(map)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribe(OrderListViewModel.this);

                    }else if(R.id.item_allorder_confirm == view.getId()){
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("orderId",orderListAdapter.getItem(position).getId());
                        m.requestConfirm(map)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribe(OrderListViewModel.this);
                    }else if(R.id.item_allorder_pay == view.getId()){
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("orderId",orderListEntity.getData().get(position).getId());
                        map.put("totalPrice",orderListEntity.getData().get(position).getTotalPrice());
                        m.requestpaySign(map)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribe(OrderListViewModel.this);

                    }

                }
            });
        }
        if(baseEntity instanceof PaysinEntity){
            PaysinEntity paySignEntity = (PaysinEntity) baseEntity;
            HashMap<String, Object> map = new HashMap<>();
            Bundle bundle = new Bundle();
            bundle.putString("paysign",paySignEntity.getData());
            LogUtils.e("PaysinEntity"+paySignEntity.getData());
            map.put(UIField.PAGEBUNDLE,bundle);
            map.put(UIField.PAGECLAZZ, PayActivity.class);
            startPage(map);

        }

        if(baseEntity instanceof CancelEntity){
            showMsg("取消订单成功");
//            IntentAction intentAction = new IntentAction();
//            intentAction.ation = "cancel";
//            EventBus.getDefault().post(intentAction);
        }

        if(baseEntity instanceof ConfirmEntity){
            showMsg("收货成功");
//            IntentAction intentAction = new IntentAction();
//            intentAction.ation = "confirm";
//            EventBus.getDefault().post(intentAction);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void cancel(IntentAction intentAction){
        if(intentAction.ation.equals("cancel")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("orderStatus",0);
            hashMap.put("userId",list.get(0).getUser_id());
            m.requestObserList(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }
    }

    @Subscribe(sticky = true)
    public void ispaysign(IntentAction intentAction){
        if(intentAction.ation.equals("isPay")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("orderStatus",0);
            hashMap.put("userId",list.get(0).getUser_id());
            m.requestObserList(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }
    }



}
